package com.stox.charting.ui.swing.presenter.delegate.advisor;

import java.awt.Point;

import com.stox.charting.ui.swing.view.ChartingInternalFrameView;
import com.stox.navigator.ui.swing.view.NavigatorView;

public class ChartingInternalFrameViewPositionAdvisor {

	private NavigatorView navigatorView;
	
	public void advise(ChartingInternalFrameView chartingInternalFrameView,
			int count, int index, int rows, int columns){
		if(chartingInternalFrameView != null){
			int width = chartingInternalFrameView.getParent().getSize().width;
			int height = chartingInternalFrameView.getParent().getSize().height;
			int x = navigatorView.getSize().width + navigatorView.getLocation().x;
			int y = 0;
			if(navigatorView != null && navigatorView.isVisible()
					&& chartingInternalFrameView.getParent().getSize().width > x*2){
				width = chartingInternalFrameView.getParent().getSize().width - x;
				height = chartingInternalFrameView.getParent().getSize().height;
			}else{
				x = 0;
			}
			
			height = height/rows;
			width = width/columns;
			
			int row = 1;
			int column = 1;
			int counter = 1;
			boolean shouldBreak = false;
			for(int i = 1; i <= rows; i++){
				for(int j = 1; j <= columns; j++){
					if(counter == index){
						row = i;
						column = j;
						shouldBreak = true;
						break;
					}
					counter++;
				}
				if(shouldBreak){
					break;
				}
			}
			
			chartingInternalFrameView.setLocation(new Point(x+(column-1)*width, y+(row-1)*height));
		}
	}
	
	public void setNavigatorView(NavigatorView navigatorView) {
		this.navigatorView = navigatorView;
	}
	
}
